package pres.liko.iot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pres.liko.iot.api.CacheOpenfrignClient;
import pres.liko.iot.service.CacheService;
import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.Set;

/**
 * @ClassName: CacheController
 * @Auther: liko
 * @Description:
 * @Date: 2022/10/31 21:08
 */
@RestController
@RequestMapping("/cache")
public class CacheController implements CacheOpenfrignClient {

    @Autowired
    private CacheService service;

    /**
     * String 设置String类型
     *
     * @param key
     * @param value
     * @return
     */
    @Override
    @PostMapping("/string/set/{key}/{value}")
    public Mono<Boolean> set(@PathVariable String key, @PathVariable String value) {
        return service.set(key, value);
    }

    /**
     * String 根据key获取值
     *
     * @param key
     * @return
     */
    @Override
    @PostMapping("/string/get/{key}")
    public Mono<String> get(@PathVariable String key) {
        return service.get(key);
    }

    /**
     * String 设置过期时间
     *
     * @param key
     * @param value
     * @param expireTime
     * @return
     */
    @Override
    @PostMapping("/string/set/{key}/{value}/{expireTime}")
    public Mono<Boolean> setValueWithExp(@PathVariable String key, @PathVariable String value, @PathVariable Long expireTime) {
        return service.setValueWithExp(key, value, expireTime);
    }

    /**
     * Srting 自增带步长
     *
     * @param key
     * @param step
     * @return
     */
    @Override
    @GetMapping("/string/get/{key}/{step}")
    public Mono<Long> incrementBy(@PathVariable String key, @PathVariable Long step) {
        return service.incrementBy(key, step);
    }

    /**
     * String 只有当前key不存在的时候设置值
     *
     * @param key
     * @param value
     * @return
     */
    @Override
    @PostMapping("/string/nx/{key}/{value}")
    public Mono<Boolean> setNx(@PathVariable String key, @PathVariable String value) {
        return service.setNx(key, value);
    }

    /**
     * set 添加
     *
     * @param key
     * @param fieId
     * @param value
     * @return
     */
    @Override
    @PostMapping("/hash/{key}/{fieId}/{value}")
    public Mono<Boolean> hSet(@PathVariable String key, @PathVariable String fieId, @PathVariable String value) {
        return service.hSet(key, fieId, value);
    }

    /**
     * hash 添加
     *
     * @param key
     * @param values
     * @return
     */
    @Override
    @PostMapping("/hash/{key}")
    public Mono<Boolean> hMset(@PathVariable String key, @RequestBody Map values) {
        return service.hMset(key, values);
    }

    /**
     * hash 根据key获取
     *
     * @param key
     * @return
     */
    @Override
    @GetMapping("/hash/{key}")
    public Mono<Map> gGetAll(@PathVariable String key) {
        return service.hGetAll(key);
    }

    /**
     * set 添加
     *
     * @param key
     * @param member
     * @return
     */
    @Override
    @PostMapping("/set/{key}/{member}")
    public Mono<Long> sAdd(@PathVariable String key, @PathVariable String member) {
        return service.sAdd(key, member);
    }

    /**
     * set 批量添加
     *
     * @param key
     * @param members
     * @return
     */
    @Override
    @PostMapping("/sets/{key}")
    public Mono<Long> sAddMembers(@PathVariable String key, @RequestBody Set<String> members) {
        return service.sAddMembers(key, members);
    }

    /**
     * set 根据key获取
     *
     * @param key
     * @return
     */
    @Override
    @GetMapping("/set/{key}")
    public Mono<Set<String>> sget(@PathVariable String key) {
        return service.sget(key);
    }

    /**
     * set 删除
     *
     * @param keys
     * @return
     */
    @Override
    @DeleteMapping("/set/delete")
    public Mono<Long> deleteKeys(String... keys) {
        return service.sdeleteKeys(keys);
    }

    @Override
    @PostMapping("/setmember/{key}/{value}")
    public Mono<Boolean> sIsMember(@PathVariable String key, @PathVariable String value) {
        return service.sIsMember(key, value);
    }
}
